#include <iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

#define T3_2_3 1
#include "cctype"

int main()
{
#ifdef T3_2_1
    cout << "string 的声明 " << endl;   
    string s1;
    string s2(s1);
    string s2 = s1;
    string s3("value");  //直接初始化
    string s3 = "value";  //拷贝初始化
    string s4(10, 'j');     //直接初始化
#elif T3_2_2
/*
    string s;
    cin >> s; //后面遇到空格结束 > hello Ke
    cout << s << endl; // < hello
*/
/*
    string s1, s2;
    cin >> s1 >> s2;
    cout << s1 << " " << s2 << endl;
*/

/* 输入数量未知的word
    string word;
    while(cin >> word)
    {
        cout << word << endl;
    }
*/


/********** getline **********

    string line;
    while(getline(cin, line))
    {
        if(!line.empty())
        {
            auto lz = line.size();
            cout << lz << ":" << line << endl;
        }
    }
*/

/* 比较string对象 
    string s1 = "Hello";
    string s2 = "hello World";
    string s3 = "Hi";

    if(s1 > s2)
    {
        cout << "s1 > s2" << endl; 
    }else{
        cout << "s1 < s2" << endl; 
    }
    if(s1 > s3)
    {
        cout << "s1 > s3" << endl; 
    }else{
        cout << "s1 < s3" << endl; 
    }
*/

/* 字符串拼凑 */
    string s1 = "hello";
    string s2 = "world";
    s1 += " ";
    s1 += s2;
    cout << s1 << endl;

#elif T3_2_3
#if 0
////////////处理字符串中的字符
/* 1. 处理每个字符，使用范围for语句 */
    string s1 = "hello World";
    for(auto c: s1)
    {
        cout << c;

    }
    cout << endl;

    string s2 = "hello, kw!!!";
    decltype(s2.size()) punct_cnt = 0;
    for(auto c : s2)
    {
        if(ispunct(c))
        {
            punct_cnt++;
        }
    }
    cout << s2 <<":"<< punct_cnt << endl;

    string s3 = "hello world!";
    //for(auto c : s3) //不加& 改变的只是c, &c 改变的是引用的对象本体
    for(auto &c : s3) //不加& 改变的只是c, &c 改变的是引用的对象本体
    {
        c = toupper(c);
        cout << c;
    }
    cout <<"\ns3:" << s3 << endl;

    const string hexdigits = "0123456789ABCDEF";
    string result;
    string::size_type n = 0; //数组的下标类型，保证合法性

    while(cin >> n)
    {
        if(n >= hexdigits.size())
        {
            std::cerr << "error"; 
            break;
        }
        result += " ";
        result += hexdigits[n];
    }
    cout << "restlt:" << result << endl;
#endif


/* 练习题 */
//3.6 
    string s1("hello World");
    for(auto &c : s1)
    {
        c = 'X';
    }
    cout << s1 << endl;


// 3.9
    string s2;
    cout << s2[0] << endl;

//3.10
    string s3("hel,lo! wo?rld!!!");
    string res;
    for(auto &c : s3)
    {
        if(!ispunct(c))
        {
            res += c;
        }
    }
    cout << res << endl;

    const string s4 = "Keep Out";
    for(auto &c : s4) //auto const char
    {
        //c = 'a';
        cout << c;
    }
    cout << endl;
#endif

return 0;
}
